package com.uikoo9.manage.y.model;

import com.jfinal.plugin.activerecord.Model;
import com.uikoo9.util.core.annotation.QTable;
import com.uikoo9.util.core.data.QStringUtil;
import com.uikoo9.util.function.QCacheUtil;
import com.uikoo9.util.jfinal.ucenter.model.UcenterMenuModel;
import com.uikoo9.util.plugin.contants.QContants;

import java.util.List;
import java.util.Map;

/**
 * YSettingModel<br>
 * id	id<br>
 * y_setting_name	名称<br>
 * cdate	创建时间<br>
 * cuser_id	input_id<br>
 * y_setting_val	值<br>
 * cuser_name	input_name<br>
 * @author qiaowenbin
 */
@QTable("t_y_setting")
@SuppressWarnings("serial")
public class YSettingModel extends Model<YSettingModel>{
	
	public static final YSettingModel dao = new YSettingModel();
	
	/**
	 * find all
	 * @return
	 */
	public List<YSettingModel> findAll(){
		return findAll(null);
	}
	
	/**
	 * find all by order
	 * @param order
	 * @return
	 */
	public List<YSettingModel> findAll(String order){
		StringBuilder sb = new StringBuilder("select * from t_y_setting ");
		if(QStringUtil.isEmpty(order)){
			return dao.find(sb.append("order by id desc").toString());
		}else{
			return dao.find(sb.append(order).toString());
		}
	}

    /**
     * find all by cache
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<UcenterMenuModel> findAllByCache(Map map){
        List<UcenterMenuModel> menus = null;

        String _key = "menus";
        String sql = "select * from t_ucenter_menu where 1=1 and ucenter_menu_type=? ";
        String _type = QContants.YESNO_NO;
        if("true".equals(map.get("manage")+"")){
            _type = QContants.YESNO_YES;
            sql += " and ucenter_menu_parent_id=0 ";
            _key+="-a";
        }else {
            sql += " and ucenter_menu_parent_id=(select id from t_ucenter_menu where ucenter_menu_title = 'fore' and ucenter_menu_parent_id=0) ";
        }
        sql += " order by ucenter_menu_sn ";
        Object menusObject = QCacheUtil.getFromEHCache(_key);
        if(menusObject == null){
            menus = UcenterMenuModel.dao.find(sql,_type );
            QCacheUtil.putToEHCache(_key, menus);
        }else{
            menus = (List<UcenterMenuModel>) menusObject;
        }

        return menus;
    }
	
}
